package com.zgkxzx.mvvmbase.view.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.zgkxzx.mvvmbase.R;
import com.zgkxzx.mvvmbase.bean.picture.PictureBean;
import com.zgkxzx.mvvmbase.databinding.ListItemPicturesBinding;
import com.zgkxzx.mvvmbase.viewmodel.ItemViewModel;

import java.util.List;


/**
 * Created by Administrator on 2016/9/8.
 */
public class PicturesAdapter extends RecyclerView.Adapter<PicturesAdapter.PictureViewHolder> {

    private final static String TAG = PicturesAdapter.class.getSimpleName();

    private Context context;
    private List<PictureBean> pictureList;
    private LayoutInflater inflater;


    public PicturesAdapter(Context context, List<PictureBean> pictureList) {
        this.context = context;
        this.pictureList = pictureList;
        inflater = LayoutInflater.from(context);
    }


    @Override
    public PictureViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ListItemPicturesBinding binding = DataBindingUtil.inflate(inflater, R.layout.list_item_pictures, parent, false);
        return new PictureViewHolder(binding);
    }

    @Override
    public int getItemCount() {
        return pictureList == null ? 0 : pictureList.size();
    }

    @Override
    public void onBindViewHolder(final PictureViewHolder holder, int position) {
        PictureBean pictureBean = pictureList.get(position);
        holder.setBindData(pictureBean);
    }


    class PictureViewHolder extends RecyclerView.ViewHolder {

        ListItemPicturesBinding binding;

        public PictureViewHolder(ListItemPicturesBinding binding) {
            super(binding.llCard);
            this.binding = binding;
        }

        public void setBindData(PictureBean pictureBean) {
            if (binding.getItemViewModel() == null)
                binding.setItemViewModel(new ItemViewModel(context, pictureBean));
            else
                binding.getItemViewModel().setPictureBeanData(pictureBean);
        }
    }
}
